import { Provide } from '@midwayjs/core'
import { Repository } from 'typeorm'
import { InjectEntityModel } from '@midwayjs/typeorm'
import { Admin } from '../../entities/admin.entity'

@Provide()
export class AdminService {
  @InjectEntityModel(Admin)
  repo!: Repository<Admin>

  async list(page: number = 1, pageSize: number = 10) {
    const [data, total] = await this.repo.findAndCount({
      skip: (page - 1) * pageSize,
      take: pageSize,
      order: { id: 'DESC' } // 可选排序
    })

    return {
      total,
      items: data,
    }
  }

  async add(data: Partial<Admin>) {
    const obj = this.repo.create(data)
    return this.repo.save(obj)
  }

  async edit(data: Partial<Admin>) {
    return this.repo.save(data)
  }

  async delete(id: number) {
    return this.repo.delete(id)
  }
}
